package com.inspur.edp.common.expr.visitor.field;

import com.inspur.edp.common.expr.ast.ASTContext;
import com.inspur.edp.common.expr.ast.CompileUnitParser;
import com.inspur.edp.common.expr.ast.CompileUnitVisitor;
import com.inspur.edp.common.expr.parser.ExpressAstParser;
import com.inspur.edp.common.expr.spi.FieldVisitor;
import com.inspur.edp.common.expr.visitor.VisitResult;
import com.inspur.edp.common.type.DecorationOption;
import com.inspur.edp.common.type.Field;
import com.inspur.edp.common.type.TypeRefer;
import com.inspur.edp.common.type.constant.TypeConstant;
import com.inspur.edp.common.type.parser.TypeParser;


/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/7/16 9:07
 */
public class JsonFieldVisitor implements FieldVisitor {

    public static final String KIND = TypeConstant.JSON;

    @Override
    public String getKind() {
        return KIND;
    }

    @Override
    public VisitResult visitGetter(CompileUnitVisitor visitor,
                                   ASTContext context,
                                   CompileUnitParser.FieldAccessExprContext fieldAccessExprContext,
                                   CompileUnitParser.ExpressContext objectExpr,
                                   Field field) {
        TypeRefer objectType = ExpressAstParser.getOriginalType(objectExpr, context);
        TypeRefer fieldType = TypeParser.getFieldType(objectType, field.getCode());
        VisitResult result = new VisitResult();
        String script = null;
        String objectExprDsl = visitor.visitExpress(objectExpr).toString();
        String fieldCode = field.getCode();
        //基本类型、数组类型、对象类型
        if (fieldType.isNumber()) {
            script = String.format("JsonDataUtils.getDouble(%s,\"%s\")", objectExprDsl, fieldCode);
        } else if (fieldType.isString()) {
            script = String.format("JsonDataUtils.getString(%s,\"%s\")", objectExprDsl, fieldCode);
        } else if (fieldType.isBoolean()) {
            script = String.format("JsonDataUtils.getBoolean(%s,\"%s\")", objectExprDsl, fieldCode);
        } else if (fieldType.isOffsetDateTime()) {
            //分两种情况1、date、2、dateTime
            DecorationOption decorationOption = field.getDecorationMap().get(Field.JSON_DESERIALIZER_NAME);
            String kind = decorationOption.getValue(TypeConstant.KIND);
            if (kind.equals(TypeConstant.JSON_STRING_TO_DATE)) {
                script = String.format("JsonDataUtils.getDate(%s,\"%s\")", objectExprDsl, fieldCode);
            } else {
                script = String.format("JsonDataUtils.getDateTime(%s,\"%s\")", objectExprDsl, fieldCode);
            }
        } else if (fieldType.isIterable()) {
            script = String.format("JsonDataUtils.getArray(%s,\"%s\")", objectExprDsl, fieldCode);
        } else {
            script = String.format("JsonDataUtils.getObject(%s,\"%s\")", objectExprDsl, fieldCode);
        }
        result.setScript(script);
        result.addImport("com.inspur.edp.common.type.utils.JsonDataUtils");
        return result;
    }

    @Override
    public VisitResult visitSetter(CompileUnitVisitor visitor,
                                   ASTContext context,
                                   CompileUnitParser.ExpressContext objectExpr,
                                   Field field,
                                   CompileUnitParser.ExpressContext valueExpr) {
        String objectExprDsl = visitor.visitExpress(objectExpr).toString();
        String valueExprDsl = visitor.visitExpress(valueExpr).toString();
        VisitResult result = new VisitResult(String.format("JsonPropUtil.setPropValue(%s,\"%s\",%s)", objectExprDsl, field, valueExprDsl));
        result.addImport("com.inspur.edp.dsl.core.util.JsonPropUtil");
        return result;
    }

}
